<?php
namespace Home\Controller;

class PayPageController extends ParentController {
    protected function _initialize() {
        $hotelId = I('get.hotel');
        $this->checkLanguage();
        $this->assignHotelDefultCurrency($hotelId);
    }

    /**
     * 用户确认支付信息页面
     * @modify 彪明 2016-05-16    创建
     */
    //public function confirm() {
    public function speedpay() {
        // 判断是微信返回数据还是支付宝返回数据
        $wechatParam = I('get.wechatJSON');
        if (!empty($wechatParam)) {
            $wechatParam = json_decode(stripslashes(htmlspecialchars_decode($wechatParam)), true);
        }

        $param = I('get.');

        $userId = session(C('USER_ID'));
        $hotelId = $param['hotel'];
        $orderHotel = isset($param['orderHotel']) && !empty($param['orderHotel']) ? $param['orderHotel'] : $param['hotel'];
        $orderNumber = $param['no'];
        $info = isset($param['info']) ? $param['info'] : '';

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'orderHotel' => 'notnull,notempty,id',
            'no' => 'notnull,notempty',
            'pay' => 'notnull,notempty,id',
        );
        // 是否钥匙包微信支付
        $qrclietPay = isset($_COOKIE['qrclient_ali_and_wechat']) ? $_COOKIE['qrclient_ali_and_wechat'] : null;

        try {
            \Org\Util\Validate::validate($param, $validateRule);
        } catch (\Exception $e) {
            $this->assign('error', '缺少参数');
            $this->assign('hotelId', $hotelId);
            $this->assign('orderHotel', $orderHotel);
            $this->assign('bookNumber', $orderNumber);
            $this->display();
            return;
        }

        // 微信支付需要判断 用户是否登录，支付宝支付无需登录
        $payType = $param['pay'];
        $isUserLogined = parent::isLogin();
        if ($payType == '0' && !$isUserLogined) {
            $this->assign('hotelId', $hotelId);
            $this->assign('orderHotel', $orderHotel);
            $this->assign('bookNumber', $orderNumber);
            $this->assign('isUserLogined', '0');
            $this->display();
            return;
        }

        // 调用RPC接口获取订单详情
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('ORDER_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'orderNumber' => $orderNumber,
            'userId' => $userId,
            'payPagePrice' => true,
        );
        $result = $client->detail($RPCData);

        // 根据不同情况返回信息
        if ($result['httpstatus'] != 200) {
            $this->assign('error', $result['info']);
            $this->assign('hotelId', $hotelId);
            $this->assign('orderHotel', $orderHotel);
            $this->assign('bookNumber', $orderNumber);
            $this->display();
            return;
        }

        // 如果非预订状态的订单无法支付
        if ($result['data']['status'] != ORDER_BOOKED) {
            $this->assign('error', '该订单无法支付');
            $this->assign('hotelId', $hotelId);
            $this->assign('orderHotel', $orderHotel);
            $this->assign('bookNumber', $orderNumber);
            $this->display();
            return;
        }

        // 获取客栈支付信息
        $client = new \PHPRPC_Client(C('HOTEL_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            //'hotel' => $hotelId,
            'hotel' => $orderHotel, // 获取订单对应的客栈支付信息
        );
        $payInfoResult = $client->payInfo($RPCData);
        $hasError = false;
        if (!is_array($payInfoResult)) {
            $hasError = true;
            $this->assign('error', '服务器繁忙,请稍后重试');
        }
        if ($payInfoResult['httpstatus'] != 200) {
            $hasError = true;
            $this->assign('error', $payInfoResult['info']);
        }
        if ($hasError) {
            $this->assign('hotelId', $hotelId);
            $this->assign('orderHotel', $orderHotel);
            $this->assign('bookNumber', $orderNumber);
            $this->display();
            return;
        }
        
        $payLogic = A('Common/Pay', 'Logic');
        //$payTypeStatus = $payLogic->getPayType($hotelId, (int)$payInfoResult['data']['type']);
        $payTypeStatus = $payLogic->getPayType($orderHotel, (int)$payInfoResult['data']['type']);

        // 微信支付
        if ($payType == 0) {
            if (!$payTypeStatus['wechat']) {
                if (!$payTypeStatus['alipay']) {
                    $this->assign('error', '该支付功能未启用');
                }
                $this->assign('hotelId', $hotelId);
                $this->assign('orderHotel', $orderHotel);
                $this->assign('bookNumber', $orderNumber);
                $this->display();
                return;
            }
            // 根据客栈是畅联代收还是商户自收,准备相应的参数
            if ($payTypeStatus['uclbrt_wechat']) {
                $wechatPayConfig = C('WECHAT_PAY_CONFIG');
                $appId = $wechatPayConfig['appid'];
                $appSecret = $wechatPayConfig['appsecret'];
                $mchId = $wechatPayConfig['mchid'];
                $mchKey = $wechatPayConfig['mchkey'];
            } else {
                $appId = $payInfoResult['data']['partnerPayInfo']['wechat']['appid'];
                $appSecret = $payInfoResult['data']['partnerPayInfo']['wechat']['appsecret'];
                $mchId = $payInfoResult['data']['partnerPayInfo']['wechat']['mchid'];
                $mchKey = $payInfoResult['data']['partnerPayInfo']['wechat']['mchkey'];
            }

            // 钥匙包微信支付配置 并且是畅联代收
            if (!is_null($qrclietPay) && $payTypeStatus['uclbrt_wechat']) {
                $qrWechatPayConfig = C('QR_WECHAT_PAY_CONFIG');
                $appId = $qrWechatPayConfig['appid'];
                $appSecret = $qrWechatPayConfig['appsecret'];
                $mchId = $qrWechatPayConfig['mchid'];
                $mchKey = $qrWechatPayConfig['mchkey'];
            }

            $userId = session(C('USER_ID'));
            //①、获取用户openid
            Vendor('Wechat.JsApiPay');
            $tools = new \JsApiPay($appId, $appSecret);
            $openId = $wechatParam['openid'];

            // 算出订单总价
            $totalPrice = $result['data']['payPagePrice'];
            //②、统一下单
            $input = array();
            $input['body'] = $result['data']['hotelName'] . ' ' . $result['data']['roomTypeName'] . '-' . $result['data']['roomName'];
            $input['attach'] = $userId;
            $input['out_trade_no'] = $orderNumber;
            $input['total_fee'] = $totalPrice * 100;
            $input['time_start'] = date("YmdHis");
            $input['time_expire'] = date("YmdHis", time() + 600);
            $input['notify_url'] = C('WECHAT_PAY_NOTIFY_URL');
            $input['trade_type'] = 'JSAPI';
            $input['openId'] = $openId;
            $input['appId'] = $appId;
            $input['mchId'] = $mchId;
            $input['mchKey'] = $mchKey;

            // 钥匙包微信支付 并且是畅联代收
            if (!is_null($qrclietPay) && $payTypeStatus['uclbrt_wechat']) {
                $input['trade_type'] = 'APP';
                $input['qrclietPay'] = $qrclietPay;
            }

            Vendor('Wechat.WxPayApi');
            $order = \WxPayApi::unifiedOrder($input);
            // 检查是否统一下单成功
            if (!empty($order['err_code'])) {
                $this->assign('error', $order['err_code_des']);
                $this->assign('hotelId', $hotelId);
                $this->assign('orderHotel', $orderHotel);
                $this->assign('bookNumber', $orderNumber);
                $this->display();
                return;
            }

            $payParams = $tools->GetJsApiParameters($order, $mchKey);
            $jssdk = new \Org\Util\JSSDK($appId, $appSecret);
            $signPackage = $jssdk->GetSignPackage();

            // 钥匙包微信支付 并且是畅联代收
            if (!is_null($qrclietPay) && $payTypeStatus['uclbrt_wechat']) {
                $qrclientPayParams = array(
                    'appid' =>  $order['appid'], // 应用id
                    'partnerid' =>  $order['mch_id'], // 商户号 
                    'prepayid' => $order['prepay_id'], // 预支付交易会话id
                    'noncestr' => $order['nonce_str'], // 随机字符串
                    'timestamp' => (string)time(), // 时间戳
                    'package' => 'Sign=WXPay', // 扩展字段
                );

                // app微信起调支付需要生成二次签名
                $qrclientPayParams['sign'] = $this->MyMakeSign($qrclientPayParams, $mchKey);
                $this->assign('qrclientPayParams', json_encode($qrclientPayParams));
            } else {
                $this->assign('qrclientPayParams', '');
            }

            $this->assign('signParams', $signPackage);
            $this->assign('payParams', $payParams);
        }else if ($payType == 1) {
            // 支付宝支付
            if (is_null($qrclietPay)) {
                if (!$payTypeStatus['alipay']) {
                    $this->assign('error', '该支付功能未启用');
                    $this->assign('hotelId', $hotelId);
                    $this->assign('orderHotel', $orderHotel);
                    $this->assign('bookNumber', $orderNumber);
                    $this->display();
                    return;
                }
            } else if (!$payTypeStatus['uclbrt_wechat']) {
                // 从钥匙包进入 并且关闭支付宝支付 则不提示 该支付功能未启用
                if (!$payTypeStatus['alipay']) {
                    $this->assign('error', '该支付功能未启用');
                    $this->assign('hotelId', $hotelId);
                    $this->assign('orderHotel', $orderHotel);
                    $this->assign('bookNumber', $orderNumber);
                    $this->display();
                    return;
                }
            }
        } else if ($payType == 2) {
            // 会员卡支付
            if (!$payTypeStatus['vipcardpay']) {
                $this->assign('error', '该支付功能未启用');
                $this->assign('hotelId', $hotelId);
                $this->assign('orderHotel', $orderHotel);
                $this->assign('bookNumber', $orderNumber);
                $this->display();
                return;
            }
        } else {
            //其他
            $this->assign('error', '该支付功能未启用');
            $this->assign('hotelId', $hotelId);
            $this->assign('orderHotel', $orderHotel);
            $this->assign('bookNumber', $orderNumber);
            $this->display();
            return;
        }

        $orderInfo= array(
            'hotelName' => $result['data']['hotelName'],
            'roomTypeName' => $result['data']['roomTypeName'],
            'roomName' => $result['data']['roomName'],
            'price' => $result['data']['payPagePrice'],
        );

        $this->assign('alierror', $info);
        $this->assign('hotelId', $hotelId);
        $this->assign('orderHotel', $orderHotel);
        $this->assign('payType', $payType);
        $this->assign('bookNumber', $orderNumber);
        $this->assign('orderInfo', $orderInfo);
        $this->assign('isUserLogined', '1');
        $this->display();
    }
    
    /**
     * 用户确认支付信息页面
     * @modify 彪明 2016-05-16    创建
     */
    public function qrclientSpeedPay() {
        // 判断是微信返回数据还是支付宝返回数据
        $param = I('param.');

        $userId = session(C('USER_ID'));
        $hotelId = $param['hotel'];
        $orderHotel = isset($param['orderHotel']) && !empty($param['orderHotel']) ? $param['orderHotel'] : $param['hotel'];
        $orderNumber = $param['no'];
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'orderHotel' => 'notnull,notempty,id',
            'no' => 'notnull,notempty',
            'pay' => 'notnull,notempty,id',
        );
        // 是否钥匙包微信支付
        $qrclietPay = isset($_COOKIE['qrclient_ali_and_wechat']) ? $_COOKIE['qrclient_ali_and_wechat'] : null;

        try {
            \Org\Util\Validate::validate($param, $validateRule);
        } catch (\Exception $e) {
            returnJson(403, 001, '缺少参数');
            return;
        }

        // 微信支付需要判断 用户是否登录，支付宝支付无需登录
        $payType = $param['pay'];
        $isUserLogined = parent::isLogin();
        if ($payType == '0' && !$isUserLogined) {
            returnJson(403, 001, '用户未登录');
            return;
        }

        // 调用RPC接口获取订单详情
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('ORDER_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'orderNumber' => $orderNumber,
            'userId' => $userId,
            'payPagePrice' => true,
        );
        $result = $client->detail($RPCData);

        // 根据不同情况返回信息
        if ($result['httpstatus'] != 200) {
            returnJson(403, 001, $result['info']);
            return;
        }

        // 如果非预订状态的订单无法支付
        if ($result['data']['status'] != ORDER_BOOKED) {
            returnJson(403, 001, '该订单无法支付');
            return;
        }

        // 获取客栈支付信息
        $client = new \PHPRPC_Client(C('HOTEL_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            //'hotel' => $hotelId,
            'hotel' => $orderHotel, // 获取订单对应的客栈支付信息
        );
        $payInfoResult = $client->payInfo($RPCData);
        if (!is_array($payInfoResult)) {
            returnJson(403, 001, '服务器繁忙,请稍后重试');
            return;
        }
        if ($payInfoResult['httpstatus'] != 200) {
            returnJson(403, 001, $payInfoResult['info']);
            return;
        }
        
        $payLogic = A('Common/Pay', 'Logic');
        //$payTypeStatus = $payLogic->getPayType($hotelId, (int)$payInfoResult['data']['type']);
        $payTypeStatus = $payLogic->getPayType($orderHotel, (int)$payInfoResult['data']['type']);

        // 微信支付
        if ($payType == 0) {
            if (!$payTypeStatus['wechat']) {
                returnJson(403, 001, '该支付功能未启用');
                return;
            }
            // 根据客栈是畅联代收还是商户自收,准备相应的参数
            if ($payTypeStatus['uclbrt_wechat']) {
                $wechatPayConfig = C('WECHAT_PAY_CONFIG');
                $appId = $wechatPayConfig['appid'];
                $appSecret = $wechatPayConfig['appsecret'];
                $mchId = $wechatPayConfig['mchid'];
                $mchKey = $wechatPayConfig['mchkey'];
            } else {
                $appId = $payInfoResult['data']['partnerPayInfo']['wechat']['appid'];
                $appSecret = $payInfoResult['data']['partnerPayInfo']['wechat']['appsecret'];
                $mchId = $payInfoResult['data']['partnerPayInfo']['wechat']['mchid'];
                $mchKey = $payInfoResult['data']['partnerPayInfo']['wechat']['mchkey'];
            }

            // 钥匙包微信支付配置 并且是畅联代收
            if (!is_null($qrclietPay) && $payTypeStatus['uclbrt_wechat']) {
                $qrWechatPayConfig = C('QR_WECHAT_PAY_CONFIG');
                $appId = $qrWechatPayConfig['appid'];
                $appSecret = $qrWechatPayConfig['appsecret'];
                $mchId = $qrWechatPayConfig['mchid'];
                $mchKey = $qrWechatPayConfig['mchkey'];
            }

            $userId = session(C('USER_ID'));
            //①、获取用户openid
            Vendor('Wechat.JsApiPay');
            $tools = new \JsApiPay($appId, $appSecret);
            $openId = $wechatParam['openid'];

            // 算出订单总价
            $totalPrice = $result['data']['payPagePrice'];
            //②、统一下单
            $input = array();
            $input['body'] = $result['data']['hotelName'] . ' ' . $result['data']['roomTypeName'] . '-' . $result['data']['roomName'];
            $input['attach'] = $userId;
            $input['out_trade_no'] = $orderNumber;
            $input['total_fee'] = $totalPrice * 100;
            $input['time_start'] = date("YmdHis");
            $input['time_expire'] = date("YmdHis", time() + 600);
            $input['notify_url'] = C('WECHAT_PAY_NOTIFY_URL');
            $input['trade_type'] = 'JSAPI';
            $input['openId'] = $openId;
            $input['appId'] = $appId;
            $input['mchId'] = $mchId;
            $input['mchKey'] = $mchKey;

            // 钥匙包微信支付 并且是畅联代收
            if (!is_null($qrclietPay) && $payTypeStatus['uclbrt_wechat']) {
                $input['trade_type'] = 'APP';
                $input['qrclietPay'] = $qrclietPay;
            }

            Vendor('Wechat.WxPayApi');
            $order = \WxPayApi::unifiedOrder($input);
            // 检查是否统一下单成功
            if (!empty($order['err_code'])) {
                returnJson(403, 001, $order['err_code_des']);
                return;
            }

            $payParams = $tools->GetJsApiParameters($order, $mchKey);
            $jssdk = new \Org\Util\JSSDK($appId, $appSecret);
            $signPackage = $jssdk->GetSignPackage();

            // 钥匙包微信支付 并且是畅联代收
            if (!is_null($qrclietPay) && $payTypeStatus['uclbrt_wechat']) {
                $qrclientPayParams = array(
                    'appid' =>  $order['appid'], // 应用id
                    'partnerid' =>  $order['mch_id'], // 商户号 
                    'prepayid' => $order['prepay_id'], // 预支付交易会话id
                    'noncestr' => $order['nonce_str'], // 随机字符串
                    'timestamp' => (string)time(), // 时间戳
                    'package' => 'Sign=WXPay', // 扩展字段
                );

                // app微信起调支付需要生成二次签名
                $qrclientPayParams['sign'] = $this->MyMakeSign($qrclientPayParams, $mchKey);
                $payData['qrclientPayParams'] = json_encode($qrclientPayParams);
            } else {
                $payData['qrclientPayParams'] = '';
            }

            $payData['signParams'] = $signPackage;
            $payData['payParams'] = $payParams;
        }else if ($payType == 1) {
            // 支付宝支付
            if (!$payTypeStatus['alipay']) {
                returnJson(403, 001, '该支付功能未启用');
                return;
            }            
        }else {
            //其他
            returnJson(403, 001, '该支付功能未启用');
            return;
        }

        returnJson(200, 200, $payData);
    }

    /**
     * 支付结果显示页面
     * @modify 彪明 2016-05-16    创建
     */
    public function result() {
        $param = I('get.');

        $status = $param['status'];
        $hotelId = $param['hotel'];
        $orderNumber = $param['no'];
        $time = time();
        $info = '';

        switch ($status) {
            case '1':
                //redirect("/Home/PayPage/speedpay.html?hotel={$hotelId}&no={$orderNumber}&status={$status}&time={$time}");
                $info = '支付失败';
                $paySuccess = false;
                break;
            case '2':
                //redirect("/Home/PayPage/speedpay.html?hotel={$hotelId}&no={$orderNumber}&status={$status}&time={$time}");
                $info = '用户取消支付';
                $paySuccess = false;
                break;
            default:
                //redirect("/Home/OrderPage/index.html?hotel={$hotelId}&no={$orderNumber}");
                $info = '支付成功';
                $paySuccess = true;
                break;
        }

        // 用户是否登录
        $isUserLogined = parent::isLogin();
        if (!$isUserLogined) {
            $this->assign('isUserLogined', '0');
        }
        $this->assign('isUserLogined', '1');
        // 如果存在错
        if (!$paySuccess) {

            $this->assign('speedpayerror', $info);
            $this->assign('hotelId', $hotelId);
            $this->assign('bookNumber', $orderNumber);
            //dump(0);die;
            $url = U("Home/PayPage/speedpay");
            //dump($url."?hotel={$hotelId}&no={$orderNumber}");die;
            redirect($url."?hotel={$hotelId}&no={$orderNumber}&info={$info}&pay=1&orderHotel={$hotelId}");
            return;
            //$this->display('PayPage:speedpay');
            //return;
        }
        
        redirect("/Home/OrderPage/index.html?hotel={$hotelId}&no={$orderNumber}");
        return;
        /*$this->assign('hotelId', $hotelId);
        $this->assign('bookNumber', $orderNumber);
        $this->assign('payResult', $paySuccess);
        $this->display('PayPage:speedpay');*/
    }

    /**
     * 生成签名
     * @modify wenhui 2017-07-13    创建
     */
    public function MyMakeSign($makeSignArray, $mchKey) {
        //签名步骤一：按字典序排序参数
        $string = "";
        $ksort = ksort($makeSignArray);
        foreach ($makeSignArray as $k => $v) {
            if ($k != "sign" && $v != "" && !is_array($v)) {
                $string .= $k . "=" . $v . "&";
            }
        }
        $string = trim($string, "&");
        //签名步骤二：在string后加入KEY
        $string = $string . "&key=" . $mchKey;
        //签名步骤三：MD5加密
        $string = md5($string);
        //签名步骤四：所有字符转为大写
        $string = strtoupper($string);
        return $string;
    }

    /**
     * 会员充值 用户确认支付信息页面
     * @modify wenhui 2018-01-18    创建
     */
    public function recharge() {
        // 判断是微信返回数据还是支付宝返回数据
        $wechatParam = I('get.wechatJSON');
        if (!empty($wechatParam)) {
            $wechatParam = json_decode(stripslashes(htmlspecialchars_decode($wechatParam)), true);
        }

        $param = I('get.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'no' => 'notnull,notempty,id',
            'pay' => 'notnull,notempty,id',
            'format' => 'notnull,notempty,decimal8_2',
        );

        if (!isset($param['pay']) && !isset($param['format'])) {
            $this->display();
            return;
        }

        try {
            \Org\Util\Validate::validate($param, $validateRule);
        } catch (\Exception $e) {
            $this->assign('error', '缺少参数');
            $this->display();
            return;
        }

        $hotel = $param['hotel'];
        $vipUserId = $param['no'];
        $payType = $param['pay'];
        $price = $param['format'] > 999999.99 ? 999999.99 : $param['format'];

        // 获取客栈支付信息
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('HOTEL_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotel' => $hotel,
        );
        $payInfoResult = $client->payInfo($RPCData);
        if (!is_array($payInfoResult)) {
            $this->assign('error', '服务器繁忙,请稍后重试');
            $this->display();
            return;
        }
        if ($payInfoResult['httpstatus'] != 200) {
            returnJson(403, 101, $payInfoResult['httpstatus'] . $payInfoResult['status'], null, null, $payInfoResult['info']);
            return;
        }
        
        $payLogic = A('Common/Pay', 'Logic');
        $payTypeStatus = $payLogic->getHotelPayType($hotel, (int)$payInfoResult['data']['type']);

        // 微信支付
        if ($payType == 0) {
            if (!$payTypeStatus['hotel_wechat']) {
                $this->assign('error', '该支付功能未启用');
                $this->display();
                return;
            }
            // 根据客栈是畅联代收还是商户自收,准备相应的参数
            $appId = $payInfoResult['data']['partnerPayInfo']['wechat']['appid'];
            $appSecret = $payInfoResult['data']['partnerPayInfo']['wechat']['appsecret'];
            $mchId = $payInfoResult['data']['partnerPayInfo']['wechat']['mchid'];
            $mchKey = $payInfoResult['data']['partnerPayInfo']['wechat']['mchkey'];

            $hotelName = D('HotelDetail')->getNameByHotel($hotel);
            $hotelName = isset($hotelName) ? $hotelName : '';
            $orderNumber = 'MZ' . getTotalMillisecond() . getRandomCode(4);
            //①、获取用户openid
            Vendor('Wechat.JsApiPay');
            $tools = new \JsApiPay($appId, $appSecret);
            $openId = $wechatParam['openid'];

            // 算出订单总价
            $totalPrice = $price;
            //②、统一下单
            $input = array();
            $input['body'] = $hotelName;
            $input['attach'] = $vipUserId;
            $input['out_trade_no'] = $orderNumber;
            $input['total_fee'] = $totalPrice * 100;
            $input['time_start'] = date("YmdHis");
            $input['time_expire'] = date("YmdHis", time() + 600);
            $input['notify_url'] = 'http://' . MEIZHU_BOOK_HOST . '/Home/Pay/vipChargeWechatPayCallback.html';
            $input['trade_type'] = 'JSAPI';
            $input['openId'] = $openId;
            $input['appId'] = $appId;
            $input['mchId'] = $mchId;
            $input['mchKey'] = $mchKey;

            Vendor('Wechat.WxPayApi');
            $order = \WxPayApi::unifiedOrder($input);
            // 检查是否统一下单成功
            if (!empty($order['err_code'])) {
                returnJson(403, 101, '', null, null, $order['err_code_des']);
                return;
            }

            $payParams = $tools->GetJsApiParameters($order, $mchKey);
            $jssdk = new \Org\Util\JSSDK($appId, $appSecret);
            $signPackage = $jssdk->GetSignPackage();

            $this->assign('signParams', $signPackage);
            $this->assign('payParams', $payParams);
            $this->assign('error', '');
            $this->display();
            return;
        } else {
            // 支付宝支付
            if (!$payTypeStatus['hotel_alipay']) {
                $this->assign('error', '该支付功能未启用');
                $this->display();
                return;
            }
        }
    }
}
